#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: pylink_main.py
Date: December 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:
An experimental Web GUI - the view

"""

#-----------------------------------------------------------------------
# System imports
# Create your views here.
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
from django.shortcuts import render_to_response
from django import forms
from django.utils import simplejson
import Pyro.core
# Application imports
# These are local for the moment as its not pathed up
import msg_q
import utils

#-----------------------------------------------------------------------
# /sdr/power
def power(request):
    if request.method == 'POST':
        xhr = request.GET.has_key('xhr')
        if xhr:
            # Its an Ajax request as we put the query string 'xhr' for ident
            # Get the event service ref from the session state
            es = request.session['es']
            # Process the command and just event into the blue yonder
            cmd = request.POST['cmd']
            if cmd == 'refresh':
                es.publish('ui_hints', ('do_hints', None))
                
            elif cmd == '6k0':
                es.publish('dsp_events', ('set_filter', '6K0'))
            elif cmd == '2k4':
                es.publish('dsp_events', ('set_filter', '2K4'))
            elif cmd == '2k1':
                es.publish('dsp_events', ('set_filter', '2K1'))
            elif cmd == '1k0':
                es.publish('dsp_events', ('set_filter', '1K0'))
            elif cmd == '500':
                es.publish('dsp_events', ('set_filter', '500'))
            elif cmd == '250':
                es.publish('dsp_events', ('set_filter', '250'))
                
            elif cmd == 'lsb':
                es.publish('dsp_events', ('set_mode', 'LSB'))
            elif cmd == 'usb':
                es.publish('dsp_events', ('set_mode', 'USB'))
            elif cmd == 'cwl':
                es.publish('dsp_events', ('set_mode', 'CW-L'))
            elif cmd == 'cwu':
                es.publish('dsp_events', ('set_mode', 'CW-U'))
            elif cmd == 'am':
                es.publish('dsp_events', ('set_mode', 'AM'))
            #elif cmd == 'nfm':
            #    es.publish('dsp_events', ('set_mode', 'NFM'))
                
            elif cmd == 'mhz100':
                es.publish('hw_events', ('freq_inc', 100))
            elif cmd == '-mhz100':
                es.publish('hw_events', ('freq_inc', -100))
            elif cmd == 'mhz10':
                es.publish('hw_events', ('freq_inc', 10))
            elif cmd == '-mhz10':
                es.publish('hw_events', ('freq_inc', -10))
            elif cmd == 'mhz1':
                es.publish('hw_events', ('freq_inc', 1))
            elif cmd == '-mhz1':
                es.publish('hw_events', ('freq_inc', -1))
            elif cmd == 'khz100':
                es.publish('hw_events', ('freq_inc', 0.1))
            elif cmd == '-khz100':
                es.publish('hw_events', ('freq_inc', -0.1))
            elif cmd == 'khz10':
                es.publish('hw_events', ('freq_inc', 0.01))
            elif cmd == '-khz10':
                es.publish('hw_events', ('freq_inc', -0.01))
            elif cmd == 'khz1':
                es.publish('hw_events', ('freq_inc', 0.001))
            elif cmd == '-khz1':
                es.publish('hw_events', ('freq_inc', -0.001))
            elif cmd == 'hz100':
                es.publish('hw_events', ('freq_inc', 0.0001))
            elif cmd == '-hz100':
                es.publish('hw_events', ('freq_inc', -0.0001))
            elif cmd == 'hz10':
                es.publish('hw_events', ('freq_inc', 0.00001))
            elif cmd == '-hz10':
                es.publish('hw_events', ('freq_inc', -0.00001))
            elif cmd == 'hz1':
                es.publish('hw_events', ('freq_inc', 0.000001))
            elif cmd == '-hz1':
                es.publish('hw_events', ('freq_inc', -0.000001))
            
            # Finally pick up the last incoming event from pylink
            return send_event()

    else:
        # Page request, return the one and only
        # Get and stash the event service ref
        es = Pyro.core.getProxyForURI("PYRONAME://"+Pyro.constants.EVENTSERVER_NAME)
        request.session['es'] = es
        # Init the message queue
        msg_q.init()
        return render_to_response('radio_console.html', {})

#-----------------------------------------------------------------------
# /sdr/poll
# sent from the client to pick up the next incoming event
def poll(request):
    if request.method == 'POST':
        xhr = request.GET.has_key('xhr')
        if xhr:
            return send_event()

#-----------------------------------------------------------------------
# Get event and return it to the client as a json string
def send_event():
    event = msg_q.get_next()
    if event <> None:
        key, data = event
        if key == 'freq_init' or key == 'freq':
            data = utils.FreqToString(data)
        return HttpResponse(simplejson.dumps({key:data}), mimetype='application/javascript')
    else:
        return HttpResponse(simplejson.dumps({}), mimetype='application/javascript')
